package org.example.my.controller;

import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.example.my.Utility.JWTUtility;
import org.example.my.Utility.SecurityUtil;
import org.example.my.model.Result;
import org.example.my.model.UserInfo;
import org.example.my.service.BlogService;
import org.example.my.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    BlogService blogService;

    @RequestMapping(value = "/login", produces = "application/json;charset=utf-8")
    public Result login(String userName, String password) {
        log.info("用户登录");
        log.info("用户名:{}", userName);
        UserInfo userInfo = userService.selectUserInfoByUserName(userName);
        if (userInfo == null) {
            return Result.fail("用户不存在");
        }
        if (!userService.isLogin(password, userInfo)) {
            return Result.fail("密码错误");
        }
            //返回令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("userName", userInfo.getUserName());
            claims.put("id", userInfo.getId());
            String token = JWTUtility.getJWTToken(claims);
            return Result.success(token);
    }

    @RequestMapping("/selectUserInfoByBlogId")
    public UserInfo selectUserInfoByBlogId(@RequestParam("id") Integer blogId) {
        log.info("根据博客id获取用户信息");
        Integer userId = blogService.selectUserIdById(blogId);
        UserInfo userInfo = userService.selectUserInfoById(userId);
        return userInfo;
    }

    @RequestMapping("/selectUserInfo")
    public UserInfo selectUserInfo(HttpServletRequest  request) {
        log.info("获取用户信息");
        String token = request.getHeader("token");
        Integer userId = JWTUtility.getUserId( token);
        return userService.selectUserInfoById(userId);
    }

    @RequestMapping("/isLoginByBlogId")
    public Boolean isLoginByBlogId(@RequestParam("id") Integer blogId, HttpServletRequest request) {
        log.info("判断文章是否是用户的");
        Integer userId = blogService.selectUserIdById(blogId);
        String token = request.getHeader("token");
        Integer loginUserId = JWTUtility.getUserId(token);
        return userId.equals(loginUserId);
    }

    @RequestMapping("/getBlogCountByUserName")
    public Integer getBlogCountByUserid(String userName) {
        log.info("获取用户文章数量");
        return userService.getBlogCountByUserid(userName);
    }

    @RequestMapping("/register")
    public Result register(UserInfo userInfo) {
        log.info("用户注册");
        if (userService.selectUserInfoByUserName(userInfo.getUserName()) != null) {
            return Result.fail("用户已存在");
        }
        userInfo.setPassword(SecurityUtil.generateSalt(userInfo.getPassword()));
        return Result.success(userService.insertUser(userInfo));
    }

}

